﻿using BarrageGameAssistant.Core.Model;
using BarrageGameAssistant.Core.Model.Base;
using Newtonsoft.Json;
using System.ComponentModel;
using System.Drawing;

namespace BarrageGameAssistant.Core.Configs
{
    public class Settings : ConfigBase
    {
        private string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "setting.json");

        /// <summary>
        /// 调试模式
        /// </summary>
        public bool Debug { get; set; }

        /// <summary>
        /// 机器识别码
        /// </summary>
        [JsonIgnore]
        public string Sn { get; private set; } = string.Empty;

        /// <summary>
        /// 许可证
        /// </summary>
        public string Licence { get; set; } = string.Empty;

        #region 目录
        /// <summary>
        /// 游戏目录
        /// </summary>
        [JsonIgnore]
        public string GameDirectory { get; private set; }

        /// <summary>
        /// 临时目录
        /// </summary>
        [JsonIgnore]
        public string TempDirectory { get; private set; }

        /// <summary>
        /// 话术目录
        /// </summary>
        [JsonIgnore]
        public string SoundScriptsDirectory { get; private set; }

        /// <summary>
        /// 欢迎话术目录
        /// </summary>
        [JsonIgnore]
        public string WelcomeScriptsDirectory { get; private set; }

        /// <summary>
        /// 拉关注话术目录
        /// </summary>
        [JsonIgnore]
        public string FansClubScriptsDirectory { get; private set; }
        #endregion

        public int CaptureFrameRate { get; set; } = 30;

        /// <summary>
        /// 画面监控器打开标记
        /// </summary>
        [JsonIgnore]
        public bool IsMonitorOpen { get; set; }

        /// <summary>
        /// 是否自动加载默认游戏配置
        /// </summary>
        public bool AutoLoadGame { get; set; } = true;

        /// <summary>
        /// 默认游戏
        /// </summary>
        public string DefaultGame { get; set; } = string.Empty;

        /// <summary>
        /// 默认分辨率 "1080x1920"
        /// </summary>
        public string DefaultResolutionRatio { get; set; } = "1080x1920";

        /// <summary>
        /// 默认分辨率 "1080x1920"
        /// </summary>
        public Size DefaultResolutionRatioSize { get; set; } = new Size(1080, 1920);

        /// <summary>
        /// 采集修正显示像素
        /// </summary>
        public int CaptureFixDisplayPixel { get; set; } = 2;

        /// <summary>
        /// 场景检测周期 默认: 5秒
        /// </summary>
        public int SceneCheckInterval { get; set; } = 5;

        /// <summary>
        /// 是否计算场景实际积分
        /// </summary>
        public bool SceneCalculateRealScore { get; set; } = false;

        #region 声音播放设置
        /// <summary>
        /// 话术声音播放设备
        /// </summary>
        public int SoundPlayDeviceId { get; set; }

        /// <summary>
        /// 话术声音播放设备
        /// </summary>
        public string SoundPlayDeviceName { get; set; } = string.Empty;

        /// <summary>
        /// 话术播放音量
        /// </summary>
        public int SoundPlayvolume { get; set; } = 100;
        #endregion

        #region 话术
        /// <summary>
        /// 全局欢迎词
        /// </summary>
        public List<LanguageScript> WelcomeScripts { get; set; }

        /// <summary>
        /// 拉关注和灯牌话术
        /// </summary>
        public List<LanguageScript> FansClubScripts { get; set; }
        #endregion

        public Settings() : base()
        {
            Sn = new Utils.HwInfoUtils().GetHwSN();
            string baseDir = AppDomain.CurrentDomain.BaseDirectory;
            GameDirectory = Path.Combine(baseDir, "Games");
            TempDirectory = Path.Combine(baseDir, "Temp");
            SoundScriptsDirectory = Path.Combine(baseDir, "SoundScripts");
            WelcomeScriptsDirectory = Path.Combine(SoundScriptsDirectory, "SoundScripts");
            FansClubScriptsDirectory = Path.Combine(SoundScriptsDirectory, "SoundScripts");
            if (!Directory.Exists(GameDirectory))
                Directory.CreateDirectory(GameDirectory);
            if (!Directory.Exists(TempDirectory))
                Directory.CreateDirectory(TempDirectory);
            if (!Directory.Exists(SoundScriptsDirectory))
                Directory.CreateDirectory(SoundScriptsDirectory);
            if (!Directory.Exists(WelcomeScriptsDirectory))
                Directory.CreateDirectory(WelcomeScriptsDirectory);
            if (!Directory.Exists(FansClubScriptsDirectory))
                Directory.CreateDirectory(FansClubScriptsDirectory);

            WelcomeScripts = [];
            FansClubScripts = [];

        }

        #region 载入和保存
        public void Save()
        {
            Save(file);
        }

        public static Settings Load()
        {
            string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "setting.json");

            var result = Load<Settings>(file);
            if (result == null)
            {
                result = new Settings();
                result.Save();
            }
            return result;
        }
        #endregion

        #region 获取游戏配置
        /// <summary>
        /// 获取默认游戏的配置
        /// </summary>
        /// <returns></returns>
        public GameProject? GetGameProject()
        {
            string prjFile = Path.Combine(GameDirectory, DefaultGame, "project.json");
            if (!File.Exists(prjFile))
                return null;

            return GameProject.Load(prjFile, false);
        }

        /// <summary>
        /// 获取指定名称的游戏配置
        /// </summary>
        /// <param name="gameName"></param>
        /// <returns></returns>
        public GameProject? GetGameProject(string gameName)
        {
            string prjFile = Path.Combine(GameDirectory, gameName, "project.json");
            if (!File.Exists(prjFile))
                return null;

            return GameProject.Load(prjFile, false);
        }
        #endregion
    }
}
